package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.CostOther;
import com.jemmyee.pizza.entity.Person;
import com.jemmyee.pizza.entity.Prepaid;
import com.jemmyee.pizza.entity.PrepaidDelete;
import com.jemmyee.pizza.service.ICostOtherService;
import com.jemmyee.pizza.service.IPrepaidDeleteService;
import com.jemmyee.pizza.service.IPrepaidService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.PrepaidBean;
import com.jemmyee.pizza.web.bean.PrepaidDeleteBean;

@Controller
@RequestMapping("/admin/prepaidDelete")
public class PrepaidDeleteController {
	
		private static Log log = LogFactory.getLog(PrepaidDeleteController.class);
		@Autowired
		private IPrepaidService<Prepaid> prepaidService;
		@Autowired
		private IPrepaidDeleteService<PrepaidDelete> prepaidDeleteService;
		@Autowired
		private ICostOtherService<CostOther> costOtherService ;
		
		@RequestMapping("/list/{from}/{startIndex}")
		public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute PrepaidDeleteBean bean,HttpServletRequest request) {
			DetachedCriteria dc = DetachedCriteria.forClass(PrepaidDelete.class);
			PrepaidDeleteBean prepaidDeleteBean = new PrepaidDeleteBean();
			if (from.equals("form")) {
				prepaidDeleteBean = bean;
				request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, prepaidDeleteBean);
			}
			if (from.equals("page")) {
				if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
					prepaidDeleteBean = (PrepaidDeleteBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
				}
			}
			if (from.equals("menu")) {
				System.out.println("menu is call");
			}

			if(prepaidDeleteBean.getName() != null && !prepaidDeleteBean.getName().equals("")){
				dc.add(Restrictions.like("prepaidName", prepaidDeleteBean.getName(), MatchMode.ANYWHERE));
			}
			
			if(prepaidDeleteBean.getNumber() != null && !prepaidDeleteBean.getNumber().equals("")){
				dc.add(Restrictions.like("prepaidId", prepaidDeleteBean.getNumber(), MatchMode.ANYWHERE));
			}
			
			if(prepaidDeleteBean.getAddr() != null && !prepaidDeleteBean.getAddr().equals("")){
				dc.add(Restrictions.like("prepaidAddress", prepaidDeleteBean.getAddr(), MatchMode.ANYWHERE));
			}
			// 发布时间
			if (prepaidDeleteBean.getStartTime() != null) {
				dc.add(Restrictions.ge("createTime", prepaidDeleteBean.getStartTime()));
			}
			if (prepaidDeleteBean.getEndTime() != null) {
				dc.add(Restrictions.le("createTime", prepaidDeleteBean.getEndTime()));
			}

//			dc.addOrder(Order.desc("createTime"));
			ModelAndView mv = new ModelAndView();
			Page page = prepaidDeleteService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"createTime");
			mv.addObject("page", page);
			mv.addObject("prepaidDeleteBean", prepaidDeleteBean);
			mv.setViewName("/admin/prepaidDelete/list");
			return mv;
		}
		
		
		@RequestMapping("/view/{id}")
		public ModelAndView view(@PathVariable Integer id) {
			ModelAndView mv = new ModelAndView();
			PrepaidDelete prepaid = prepaidDeleteService.findById(id);
			mv.setViewName("/admin/prepaidDelete/view");
			mv.addObject("prepaid",prepaid);
			return mv;
		}
		
		
		@RequestMapping("/delete/{id}")
		@ResponseBody
		public Map<String, Object> delete(@PathVariable Integer id,HttpServletRequest request)
		{   
			Map<String, Object> model = new HashMap<String, Object>();
			Prepaid p = prepaidService.findById(id);
			if(p == null){
				model.put("msg","error");
			}else{
				//System.out.println(p+"在这里 执行了 删除");
				//System.out.println(id);
				PrepaidDelete pd = new PrepaidDelete();
				pd.setPrepaidId(p.getPrepaidId());
				pd.setPrepaidName(p.getPrepaidName());
				pd.setPrepaidType(p.getPrepaidType());
				pd.setPrepaidPrice(p.getPrepaidPrice());
				pd.setPrepaidMeterType(p.getPrepaidMeterType());
				pd.setPrepaidTel(p.getPrepaidTel());
				pd.setCreateTime(p.getCreateTime());
				pd.setCreateUser(p.getCreateUser());
				pd.setRemark(p.getRemark());
				pd.setPassword(p.getPassword());
				pd.setPrepaidGqcs(p.getPrepaidGqcs());
				pd.setPrepaidGqzl(p.getPrepaidGqzl());
				pd.setPrepaidGqrq(p.getPrepaidGqrq());
				pd.setPrepaidXbzm(p.getPrepaidXbzm());
				pd.setPrepaidBqcs(p.getPrepaidBqcs());
				pd.setPrepaidFkrq(p.getPrepaidFkrq());
				pd.setPrepaidGqd(p.getPrepaidGqd());
				pd.setPrepaidKpbh(p.getPrepaidKpbh());
				pd.setPrepaidZxh(p.getPrepaidZxh());
				pd.setVipPoint(p.getVipPoint());
				pd.setMeterName(p.getMeterName());
				prepaidDeleteService.save(pd);
				prepaidService.delete(p);
				model.put("msg","succ");
			}
			return model;
		}
}
